<!--
 * @version: 
 * @Author: yu
 * @Date: 2023-07-01 19:30:16
 * @LastEditors: Andy
 * @LastEditTime: 2023-07-04 20:32:56
-->
<template>
    <div>
        <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="id" border
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <el-table-column prop="id" label="商品编号" sortable></el-table-column>
            <el-table-column prop="secondcatename" label="商品名称"></el-table-column>
            <el-table-column prop="price" label="商品价格"></el-table-column>
            <el-table-column prop="market_price" label="市场价格"></el-table-column>
            <el-table-column prop="specsname" label="图片">
                <template v-slot="item">
                    <img v-if="item.row.pid !== 0" :src="'http://localhost:3000' + item.row.img" alt="">
                </template>
            </el-table-column>
            <el-table-column prop="isnew" label="是否新品">
                <template v-slot="v">
                    <el-tag v-if="v.row.isnew === 1">是</el-tag>
                    <el-tag v-else type="danger">否</el-tag>
                </template>

            </el-table-column>
            <el-table-column prop="ishot" label="是否热卖">
                <template v-slot="v">
                    <el-tag v-if="v.row.ishot === 1">是</el-tag>
                    <el-tag v-else type="danger">否</el-tag>
                </template>
            </el-table-column>


            <el-table-column prop="status" label="状态">
                <template v-slot="item">
                    <el-button v-if="item.row.status === 1" type="primary" size="small">启用</el-button>
                    <el-button v-else type="danger" size="small">禁用</el-button>
                </template>
            </el-table-column>

            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button @click="edit(scope.row.id)" type="primary" size="small">编辑</el-button>
                    <el-button @click="del(scope.row.id)" type="danger" size="small">删除</el-button>
                </template>
            </el-table-column>

        </el-table>
        <!-- page-size每页显示条目个数，支持 .sync 修饰符  currentPage 改变时会触发   分页-->
        <el-pagination v-show="info.all" :page-size="size" @current-change="curlPage" background layout="prev, pager, next"
            :total="total">
        </el-pagination>
    </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
import { successAlert } from '@/utils/alert'
import { goodsDel } from '@/request/api'
export default {
    data() {
        return {}
    },
    props: ['info'],
    methods: {
        ...mapActions({
            requestGoods: 'goods/listActions',
            requestCount: 'goods/countActions',
            requestPage: 'goods/pageActions'
        }),
        //编辑
        edit(id) {
            this.$emit('edit', id)
        },
        //删除
        del(id) {
            this.$confirm('确定要删除码?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                goodsDel({ id }).then(res => {
                    if (res.code === 200) {
                        //消息提示
                        successAlert(res.msg)
                        //更新列表
                        this.requestGoods()
                        this.requestCount()
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        curlPage(page) {
            this.requestPage(page)
            this.requestGoods()
        }

    },
    mounted() {
        this.requestGoods()
        this.requestCount()
    },
    watch: {},
    computed: {
        ...mapGetters({
            tableData: 'goods/list',
            total: 'goods/total',
            size: 'goods/size'
        })
    },
    filters: {},
    components: {}
}
</script>
<style scoped>
img {
    width: 100px;
    height: 100px;
}
</style>